package com.example.mymenu.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mymenu.R;
import com.example.mymenu.MenuAdapter;
import com.example.mymenu.bean.MyTakeBean;
import com.example.mymenu.manager.DataManager;
import com.example.mymenu.manager.DbManager;
import com.example.mymenu.manager.NetworkManager;

import java.util.List;

public class MenuActivity extends AppCompatActivity {

    private RecyclerView secondlist;
    private DbManager dbManager;
    private DataManager dataManager;
    private NetworkManager networkManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_second);
        Intent intent = getIntent();
        String param = intent.getStringExtra("param");
        //Uri uri = Uri.parse("content://com.example.mymenu/menu");
        //内容解析者获取数据
        //ContentResolver contentResolver = getContentResolver();
        //Cursor cursor = contentResolver.query(uri,null,new String("function=?"),new String[]{param},null);
        //数据库获取数据
        dbManager = new DbManager(MenuActivity.this);
        networkManager = new NetworkManager(this);
        Cursor cursor = dbManager.query("menu",null,new String("function=?"),new String[]{param},null,null,null);
        MyTakeBean myTakeBean = null;
        Integer integer = 5;
        while (cursor.getCount() == 0 && integer > 0){
            myTakeBean = networkManager.postMenu(param);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cursor = dbManager.query("menu",null,new String("function=?"),new String[]{param},null,null,null);
            integer--;
        }
//        if(!cursor.moveToFirst()){
//            networkManager.postMenu(param);
//            //cursor = dbManager.query("menu",null,new String("function=?"),new String[]{param},null,null,null);
//        }
        //从数据管理器中获取数据
        if (cursor.getCount() != 0){
            dataManager = new DataManager();
            List<String> menuInfo = dataManager.getInfoFromCursor(cursor);
            String[] menuStep = dataManager.getStepFromCursor(cursor);
            String[] imagePic = dataManager.getStepImageFromCursor(cursor);
            cursor.close();
            //布局
            dealInfoToLayout(menuInfo);
            secondlist = findViewById(R.id.secondlist);
            secondlist.setLayoutManager(new LinearLayoutManager(MenuActivity.this));
            MenuAdapter menuAdapter = new MenuAdapter(MenuActivity.this,menuStep,imagePic);
            secondlist.setAdapter(menuAdapter);
        }else{
            finish();
            Toast.makeText(this,"数据库中无数据，请稍等一会再次点击",Toast.LENGTH_SHORT).show();

        }


    }

    private void dealInfoToLayout(List<String> menuInfo){
        TextView textView = findViewById(R.id.textView_Name);
        textView.setText(menuInfo.get(0));
        textView = findViewById(R.id.textView_Time);
        textView.setText(menuInfo.get(1));
        textView = findViewById(R.id.textView_Content);
        textView.setText(menuInfo.get(2));
        textView = findViewById(R.id.textView_Marterial);
        textView.setText(menuInfo.get(3));
    }

}